# -*- coding: utf-8 -*- 
# @project : 《Atcoder》
# @Author : created by bensonrachel on 2021/10/11
# @File : 24.D - Between Two Arrays.py
# 注意取模的地方会卡超时
modulo = 998244353
def dp_solve():
    dp = [[0]*3001 for _ in range(n)]
    pre = [0]*3001#前缀和数组，一维也行 二维也行
    for i in range(a[0],b[0]+1):
        dp[0][i] = 1
    pre[a[0]] = 1
    for i in range(a[0]+1, 3001):
        pre[i] = pre[i-1]+dp[0][i]


    for i in range(1,n):
        for j in range(a[i],b[i]+1):
            dp[i][j] = pre[j]
        #在每一组所有数都遍历结束后就计算该组的前缀和数组
        pre[a[i]] = dp[i][a[i]]
        for q in range(a[i] + 1, 3001):
            pre[q] = (pre[q - 1] + dp[i][q])%modulo

    ans = pre[3000]#取模操作如果放这里就铁超时。要放在每次的相加后。
    return ans

def dp_solve_TLE():#超时做法，没有用到前缀和
    dp = [[0]*3001 for _ in range(n)]
    for i in range(a[0],b[0]+1):
        dp[0][i] = 1
    for i in range(1,n):
        for j in range(max(a[i],b[i-1]),b[i]+1):
            dp[i][j] = sum(dp[i-1])
        for k in range(a[i],max(a[i],b[i-1])):
            dp[i][k] = sum(dp[i-1][a[i-1]:k+1])
    ans = sum(dp[-1])%modulo
    return ans
"""
整体思路做法想出来了。
要对每一组a，b中的每一个数单独做状态转移，所以要使用两个循环，第一个循环是0-n的，内层循环是每一组的a-b之间的每个数的遍历，每个数的ans都由上一轮的多个ans和得到，（多个数的方法数转移到一个数），所以需要用到前缀和，不然就是多次的sum，复杂度很大
"""
if __name__ == '__main__':
    n = int(input())
    a = [int(i) for i in input().split()]
    b = [int(i) for i in input().split()]
    ans = dp_solve()
    print(ans)
